package eteam.aps.gateway.controller;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;

/**
 * @描述：网关相关接口
 */
@CrossOrigin
@RestController
@RequestMapping("/gateaway")
public class CommonController {

 
	@PostMapping("/fileUploader ")
	public String fileUploader(@RequestPart("file") MultipartFile file, @RequestParam("bucketName") String bucketName,
			@RequestParam("objectName") String objectName) {
		try {
			// 使用MinIO服务的URL，端口，Access key和Secret key创建一个MinioClient对象
			MinioClient minioClient = MinioClient.builder().endpoint("https://play.min.io")
					.credentials("Q3AM3UQ867SPQQA43P2F", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG").build();

			// 检查存储桶是否已经存在
			boolean isExist = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
			if (isExist) {
				System.out.println("Bucket already exists.");
			} else {
				// 创建一个名为asiatrip的存储桶，用于存储照片的zip文件。
				minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
			}
			// 使用putObject上传一个文件到存储桶中。bucketName 是
			// minioClient.putObject(bucketName, objectName, file.getInputStream(),
			// file.getContentType());
			return "上传成功";
		} catch (Exception e) {
			System.out.println("Error occurred: " + e);
			return "上传失败";
		}
	}
}
